﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fluq
{
	sealed class DependencyObjectLifetime : IDependencyLifetime
	{
		private IDependencyContainer _container;
		private ICollection<object> _children = new List<object>();

		public DependencyObjectLifetime(IDependencyContainer container)
		{
			_container = container;
		}

		public ICollection<object> Children { get { return _children; } }

		public bool TryAcquire(object graphId, IDependencyContainer container, IDependency dependency, out object instance)
		{
			if (_container.Lifetime.TryAcquire(graphId, _container, dependency, out instance))
			{
				_children.Add(instance);

				return true;
			}
			else
			{
				return false;
			}
		}

		public bool TryDiscard(IDependencyContainer container, object instance)
		{
			return _container.Lifetime.TryDiscard(_container, instance);
		}

		public void DiscardAll(IDependencyContainer container)
		{
			_container.Lifetime.DiscardAll(_container);
		}
	}
}
